# Input:    /Data/finalData.RData
# Output:   /Paper/Figures/bernanke_powell_yellen.pdf
#           /Paper/Figures/windows_descriptive.pdf
# Author:   JB


rm(list = ls())
require(lme4)
require(lfe)
require(fixest)
require(tidyverse)
require(ggridges)


load('../data/finalData.RData')

# Data prep
thresholds <- utterance_level %>%
  filter(grepl('FED',opensecretsID)) %>%
  group_by(docID,chamber) %>%
  summarise(n=n(),
            Greenspan = grepl('GREENSPAN',opensecretsID),
            Yellen = grepl('YELLEN',opensecretsID),
            Bernanke = grepl('BERNANKE',opensecretsID),
            Powell = grepl('POWELL',opensecretsID)) %>%
  distinct() %>%
  ungroup() %>%
  data.frame()


toAnalDyad <- speaker_level %>%
  mutate(respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE'))


BernYellRes <- list()
dates <- list(as.Date(c('2006-01-01','2018-01-01')),
              as.Date(c('2008-01-01','2016-01-01')),
              as.Date(c('2013-01-01','2015-01-01')))

for(ds in dates) {
  for(cham in c('House','Senate')) {
    # stop()
    BernYellRes[[paste(ds,collapse = ' to ')]][[cham]] <- feols(as.formula(paste0('interruptorPct ~ factor(respondingTo)',
                                                                                 '| opensecretsID')),
                                                               toAnalDyad %>%
                                                                 filter(date > as.Date(ds[1]) & date < as.Date(ds[2]),
                                                                        chamber == cham) %>%
                                                                 filter(grepl('FED',respondingTo),
                                                                        !grepl('FED',opensecretsID)) %>%
                                                                 group_by(opensecretsID) %>%
                                                                 mutate(n = n(),
                                                                        keep = any(grepl('YELLEN',respondingTo)) & any(grepl('BERNANKE',respondingTo))) %>%
                                                                 ungroup() %>%
                                                                 filter(n > 1,
                                                                        keep)) 
  }
}

toAnalDyad <- toAnalDyad %>%
  mutate(respondingTo = relevel(factor(respondingTo),ref = 'FEDYELLEN'))
YellPowRes <- list()
dates <- list(as.Date(c('2014-01-01','2021-01-01')),
              as.Date(c('2016-01-01','2020-01-01')),
              as.Date(c('2017-01-01','2019-01-01')))
for(ds in dates) {
  for(cham in c('House','Senate')) {
    YellPowRes[[paste(ds,collapse = ' to ')]][[cham]] <- feols(as.formula(paste0('interruptorPct ~ factor(respondingTo)',
                                                                                '| opensecretsID')),
                                                              toAnalDyad %>%
                                                                filter(date > as.Date(ds[1]) & date < as.Date(ds[2]),
                                                                       chamber == cham) %>%
                                                                filter(grepl('FED',respondingTo),
                                                                       !grepl('FED',opensecretsID)) %>%
                                                                group_by(opensecretsID) %>%
                                                                mutate(n = n(),
                                                                       keep = any(grepl('YELLEN',respondingTo)) & any(grepl('POWELL',respondingTo))) %>%
                                                                ungroup() %>%
                                                                filter(n > 1,
                                                                       keep)) 
  }
}



toplot <- NULL
for(i in 1:length(YellPowRes)) {
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(YellPowRes[[i]]$House)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(YellPowRes)[i],
                               chamber = 'House',
                               chairComp = 'Powell'))
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(YellPowRes[[i]]$Senate)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(YellPowRes)[i],
                               chamber = 'Senate',
                               chairComp = 'Powell'))
}
for(i in 1:length(BernYellRes)) {
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(BernYellRes[[i]]$House)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(BernYellRes)[i],
                               chamber = 'House',
                               chairComp = 'Bernanke'))
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(BernYellRes[[i]]$Senate)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(BernYellRes)[i],
                               chamber = 'Senate',
                               chairComp = 'Bernanke'))
}

pdf('../Paper/Figures/bernanke_powell_yellen.pdf',width = 8,height = 5)
toplot %>%
  mutate(ref = gsub('factor.*?FED','',ref)) %>%
  filter((chairComp == 'Powell' & ref == 'POWELL') | (chairComp == 'Bernanke' & ref == 'YELLEN')) %>%
  mutate(period = gsub('-01-01','',gsub('to','to\n',period)),
         chairComp = ifelse(chairComp == 'Bernanke','Bernanke -> Yellen','Yellen -> Powell')) %>%
  ggplot(aes(x = period,y = est,color = chamber,shape = chamber)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  scale_shape_manual(name = 'Chamber',values = c(21,22)) + 
  geom_errorbar(aes(ymin = est - 2*se,ymax = est + 2*se),position = position_dodge(width = .2),width = 0) + 
  geom_point(position = position_dodge(width = .2),fill = 'white',size = 3) + 
  facet_grid(~chairComp,scales = 'free') + 
  theme_ridges() + 
  scale_color_manual(name = 'Chamber',values = c('grey30','grey60')) + 
  theme(strip.background = element_rect(fill = 'white',size = .5,linetype = 'solid')) + 
  xlab('Comparison Window') + ylab('Change in interruptions\nassociated with change in Fed chair')
dev.off()



# Fuck the APSR
mList <- list()
for(chamb in c('House')) { 
  for(d in names(BernYellRes)) {
    mList[[paste0(chamb,': ',d)]] <- BernYellRes[[d]][[chamb]]
  }
  for(d in names(YellPowRes)) {
    mList[[paste0(chamb,': ',d)]] <- YellPowRes[[d]][[chamb]]
  }
}

dict <- c('factor(respondingTo)FEDYELLEN' = 'Yellen (ref. Bernanke)',
             'factor(respondingTo)FEDPOWELL' = 'Powell (ref. Yellen)',
          'opensecretsID' = 'Speaker')
etable(mList,dict = dict,depvar = F,digits = 3,
       signif.code = c('***' = .001,'**' = .01,'*' = .05,'\\dag' = .1),
       file = '../Paper/Tables/fig_8_table_house.tex')

# require(stargazer)
# stargazer(mList,
#           covariate.labels = c('Yellen (ref. Bernanke)','Powell (ref. Yellen)'),
#           add.lines = list(c('Speaker FE',rep('Y',12))),
#           star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
#           keep.stat = c('n','rsq'),out = '../Paper/Tables/fig_8_table_house.tex')


mList <- list()
for(chamb in c('Senate')) { 
  for(d in names(BernYellRes)) {
    mList[[paste0(chamb,': ',d)]] <- BernYellRes[[d]][[chamb]]
  }
  for(d in names(YellPowRes)) {
    mList[[paste0(chamb,': ',d)]] <- YellPowRes[[d]][[chamb]]
  }
}

etable(mList,dict = dict,depvar = F,digits = 3,
       signif.code = c('***' = .001,'**' = .01,'*' = .05,'\\dag' = .1),
       file = '../Paper/Tables/fig_8_table_senate.tex')



# Visualize this somehow?
pdf('../Paper/Figures/windows_descriptive.pdf',width = 8,height = 5)
toAnalDyad %>%
  mutate(date = as.Date(gsub('fed|.txt','',docID))) %>%
  filter(grepl('FED',respondingTo),
         !grepl('FED',opensecretsID)) %>%
  group_by(opensecretsID) %>%
  mutate(n = n(),
         keep = any(grepl('YELLEN',respondingTo)) & any(grepl('BERNANKE',respondingTo))) %>%
  ungroup() %>%
  filter(n > 4,
         keep) %>%
  mutate(yellen = ifelse(date < as.Date('2014-01-01'),
                         'PreYellen',
                         ifelse(date < as.Date('2018-01-01'),'Yellen','PostYellen'))) %>%
  ggplot(aes(x = date,y = interruptorPct,size = denom,color = factor(yellen))) + 
  geom_vline(xintercept = as.Date(c('2006-01-01','2014-01-01','2018-01-01'))) + 
  # geom_line(size = .1) + 
  geom_point(shape = 21,alpha = .7) + 
  geom_smooth(se = T,method = 'lm',formula = y ~ poly(x,1),size = 1,aes(color = factor(yellen))) +
  facet_grid(.~chamber) + 
  theme_ridges() + 
  theme(legend.position = 'none',
        strip.background = element_rect(fill = 'white',color = 'black',size = .5,linetype = 'solid')) + 
  ylim(c(0,100)) + 
  scale_color_manual(values = c('grey40','grey40','#DDB0FF')) + 
  annotate(geom = 'text',x = as.Date(c('2003-01-01')),y = c(Inf),label = c('Greenspan'),vjust = 1,size=3.5,color = 'grey50') + 
  annotate(geom = 'text',x = as.Date(c('2010-01-01')),y = c(Inf),label = c('Bernanke'),vjust = 1,size=3.5,color = 'grey50') + 
  annotate(geom = 'text',x = as.Date(c('2016-01-01')),y = c(Inf),label = c('Yellen'),vjust = 1,color = '#C77CFF') + 
  annotate(geom = 'text',x = as.Date(c('2018-01-01')),y = c(Inf),label = c('Powell'),vjust = 1,hjust = 1,angle = 90,size=3.5,color = 'grey50') + 
  xlab('Date') + ylab('% of interactions interrupting')
dev.off()


# RR1: placebo comparing first terms of Bernanke, and first term of powell relative to Bernanke
GreenBernRes <- list()

dates <- list(as.Date(c('2001-01-01','2014-01-01')),
              as.Date(c('2002-01-01','2010-01-01')),
              as.Date(c('2005-01-01','2007-01-01')))

for(ds in dates) {
  for(cham in c('House','Senate')) {
    # stop()
    GreenBernRes[[paste(ds,collapse = ' to ')]][[cham]] <- feols(as.formula(paste0('interruptorPct ~ factor(respondingTo)',
                                                                                  '| opensecretsID')),
                                                                toAnalDyad %>%
                                                                  filter(date > as.Date(ds[1]) & date < as.Date(ds[2]),
                                                                         chamber == cham) %>%
                                                                  filter(grepl('FED',respondingTo),
                                                                         !grepl('FED',opensecretsID)) %>%
                                                                  group_by(opensecretsID) %>%
                                                                  mutate(n = n(),
                                                                         keep = any(grepl('GREENSPAN',respondingTo)) & any(grepl('BERNANKE',respondingTo))) %>%
                                                                  ungroup() %>%
                                                                  filter(n > 1,
                                                                         keep)) 
  }
}


BernPowRes <- list()
dates <- list(as.Date(c('2010-01-01','2022-01-01')),
              as.Date(c('2012-01-01','2020-01-01')),
              as.Date(c('2013-01-01','2019-01-01')))

for(ds in dates) {
  for(cham in c('House','Senate')) {
    # stop()
    BernPowRes[[paste(ds,collapse = ' to ')]][[cham]] <- feols(as.formula(paste0('interruptorPct ~ factor(respondingTo)',
                                                                                   '| opensecretsID')),
                                                                 toAnalDyad %>%
                                                                   filter(date > as.Date(ds[1]) & date < as.Date(ds[2]),
                                                                          chamber == cham) %>%
                                                                   filter(grepl('FED',respondingTo),
                                                                          !grepl('FED',opensecretsID),
                                                                          !yellenTime) %>%
                                                                   group_by(opensecretsID) %>%
                                                                   mutate(n = n(),
                                                                          keep = any(grepl('POWELL',respondingTo)) & any(grepl('BERNANKE',respondingTo))) %>%
                                                                   ungroup() %>%
                                                                   filter(n > 1,
                                                                          keep)) 
  }
}


toplot <- NULL
for(i in 1:length(GreenBernRes)) {
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(GreenBernRes[[i]]$House)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(GreenBernRes)[i],
                               chamber = 'House',
                               chairComp = 'Greenspan vs. Bernanke'))
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(GreenBernRes[[i]]$Senate)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(GreenBernRes)[i],
                               chamber = 'Senate',
                               chairComp = 'Greenspan vs. Bernanke'))
}
for(i in 1:length(BernPowRes)) {
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(BernPowRes[[i]]$House)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(BernPowRes)[i],
                               chamber = 'House',
                               chairComp = 'Bernanke vs. Powell'))
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(BernPowRes[[i]]$Senate)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(BernPowRes)[i],
                               chamber = 'Senate',
                               chairComp = 'Bernanke vs. Powell'))
}

pdf('../Paper/Figures/placeboSI.pdf',width = 8,height = 5)
toplot %>%
  mutate(ref = gsub('factor.*?FED','',ref)) %>%
  mutate(chairComp = factor(gsub(' vs. ','->',chairComp),
                            levels = c('Greenspan->Bernanke','Bernanke->Powell'))) %>%
  mutate(period = gsub('-01-01','',gsub('to','to\n',period))) %>%
  ggplot(aes(x = period,y = est,color = chamber,shape = chamber)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  scale_shape_manual(name = 'Chamber',values = c(21,22)) + 
  geom_errorbar(aes(ymin = est - 2*se,ymax = est + 2*se),position = position_dodge(width = .2),width = 0) + 
  geom_point(position = position_dodge(width = .2),fill = 'white',size = 3) + 
  facet_grid(~chairComp,scales = 'free') + 
  theme_ridges() + 
  scale_color_manual(name = 'Chamber',values = c('grey30','grey60')) + 
  theme(strip.background = element_rect(fill = 'white',size = .5,linetype = 'solid')) + 
  xlab('Comparison Window') + ylab('Change in interruptions\nassociated with change in Fed chair')
dev.off()
